package com.kws.tool.util;

import com.google.gson.Gson;
import com.kuaidi100.sdk.api.QueryTrackMap;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.utils.SignUtils;
import com.kws.common.config.dao.Express100Info;
import com.kws.common.config.dao.rq.QueryExpress100Rq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * <b>ExpressDeliveryUtil</b>
 *
 * @description: ExpressDeliveryUtil <br>
 * @date: 2024/11/7 16:23 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Component
public class ExpressDeliveryUtil {
    private static final Logger logger = LoggerFactory.getLogger(ExpressDeliveryUtil.class);

    @Value("${Express100.key}")
    private String key;

    @Value("${Express100.customer}")
    private String customer;

    public Express100Info queryExpressDelivery(QueryExpress100Rq rq){
        Express100Info response = null;
        QueryTrackReq queryTrackReq = new QueryTrackReq();
        QueryTrackParam queryTrackParam = new QueryTrackParam();
        queryTrackParam.setCom(rq.getCom());
        queryTrackParam.setNum(rq.getNum());
        queryTrackParam.setPhone(rq.getPhone());
        queryTrackParam.setFrom(rq.getFrom());
        queryTrackParam.setTo(rq.getTo());
        queryTrackParam.setResultv2("2");
        String param = new Gson().toJson(queryTrackParam);

        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(customer);
        queryTrackReq.setSign(SignUtils.querySign(param ,key,customer));

        IBaseClient baseClient = new QueryTrackMap();
        try {
            HttpResult execute = baseClient.execute(queryTrackReq);
            Gson gson = new Gson();
            String responseBody = execute.getBody();
            response = gson.fromJson(responseBody, Express100Info.class);
            if (200 == response.getStatus()){
                return response;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
